<?php

namespace IDock\Insurance\ACIC;

use IDock\Common\BaseInterface\BuilderInterface;
use IDock\Common\Tools\XMLTools;
use IDock\Http\Request;
use IDock\Http\Response;
use IDock\Insurance\ACIC\Request\AllACICRequest;
use IDock\Insurance\ACIC\Request\NonCar\NonCarInquiryRequest;
use IDock\Insurance\ACIC\Response\AllACICResponse;
use IDock\Insurance\ACIC\Response\PolicyPaymentResponse;

class Builder implements BuilderInterface
{
    protected $ip;

    protected $config;

    protected $EChannelId;

    protected $secretKey;

    protected $PCPayUrl;

    protected $WAPPayUrl;

    protected $NCGateway;

    protected $NCEChannelId;

    protected $NCSecretKey;

    protected $deptCode;

    public function getConfig(): array
    {
        return $this->config;
    }

    public function execute(Request $request): Response
    {
        $requestId = sha1(uniqid('', true));

        /*** @var $request AllACICRequest */
        $request->loadConfig($this->config);

        $request->setRequestId($requestId);

        $param = [
            'Packet' => [
                'Head' => [
                    'EChannelID' => $this->EChannelId,
                    'RequestType' => $request->getCode(),
                    'SerialNo' => $requestId
                ],
                'Body' => $this->generate($request)
            ]

        ];

        $request->setPreParameter($param);

        $this->sign($request);

        return $this->request($request);
    }

    /**
     * 支付接口
     * @param Request $request
     * @return Response
     */
    public function payment(Request $request)
    {
        $request->loadConfig($this->config);

        $requestId = sha1(uniqid('', true));

        $request->setRequestId($requestId);

        $param = $request->generate();

        $request->setBizParam($param);
        $payUrl =  $request->getPayUrl();

        $response = new PolicyPaymentResponse(200);

        $response->setUrl($payUrl);

        return  $response;
    }

    public function generate(Request $request): array
    {
        return $request->generate();
    }

    public function sign(Request $request): Request
    {
        /*** @var $request AllACICRequest */
        $bizParam = [
            'channelId' => $this->EChannelId,
            'reqXml' => XMLTools::ToXML($request->getPreParameter(), '', '<?xml version="1.0" encoding="UTF-8"?>'),
        ];

        if ($request->noCarApi) {
            $md5 = md5($this->ip . $this->NCEChannelId . $this->NCSecretKey . $bizParam['reqXml']);
        } else {
            $md5 = md5($this->ip . $this->EChannelId. $this->secretKey . $bizParam['reqXml']);
        }

        $bizParam['md5'] = $md5;

        $request->setBizParam($bizParam);

        return $request;
    }

    public function exceptionResponse($request, $errorMessage): Response
    {
        $response = $request->getResponse();

        $response = new $response;
        $response->setCode('500');
        $response->setError($errorMessage);

        return $response;
    }

    public function request(Request $request): Response
    {
        /*** @var $request AllACICRequest */
        if ($request->noCarApi) {
            $request->gateway = $this->NCGateway;
        }

        $response = $request->request($request);
        $response->setVisitor($request->getVisitor());
        $response->printerInit();
        return $response;
    }

    public function encrypt(Request $request)
    {
        // TODO: Implement encrypt() method.
    }
}


